/**
 * Blurs a background (MovieClip or Sprite) behind a transparent window (MovieClip or Sprite).
 * Based on Pixelfumes AS2 class
 * @author Devon O.
 * ibio 整理、修改
 * 2010-1-25 17:59
 */
package com.ibio8.media.image {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	/**
	 * Blurs a background (MovieClip or Sprite) behind a transparent window (MovieClip or Sprite).
	 * 
	 * @private
	 */
	public class WindowBlur {
		protected const MASK_NAME:String = "mc_mask";
		protected var m_container:DisplayObjectContainer;
		protected var m_background:DisplayObjectContainer;
		protected var m_window:DisplayObjectContainer;
		protected var m_blurredImageData:BitmapData;
		protected var m_blurredImage:Bitmap;
		protected var m_maskBox:DisplayObjectContainer;
		protected var m_blurAmount:int;
		protected var m_blur:BlurFilter;
		
		/**
		 * 构造函数
		 * @param	container	装毛玻璃层和窗口层的容器（DisplayObjectContainer）
		 * @param	background	用来做毛玻璃的背景
		 * @param	window		显示的窗口，里面可包含遮罩区域 mc_mask，若无则自动构造
		 * @param	blurAmount	需要的毛玻璃的滤镜参数
		 */
		public function WindowBlur(container:DisplayObjectContainer, background:DisplayObjectContainer, window:DisplayObjectContainer, blurAmount:int = 8) {
			m_container = container;
			m_background = background;
			m_window = window;
			m_blur = new BlurFilter(0, 0, 3);
			this.blurAmount = blurAmount;
			createMaskBox();
		}
		
		public function kill():void {
			m_background.removeChild(m_blurredImage);
			m_window.removeChild(m_maskBox);
			m_blurredImageData.dispose();
			m_blurredImage = null;
			m_blurredImageData = null;
			m_maskBox = null;
			m_blur = null;
		}
		
		public function set blurAmount(value:int):void {
			m_blurAmount = ((value >= 0) && (value <= 255)) ? value : 16;
			if (m_blur) {
				m_blur.blurX = m_blur.blurY = m_blurAmount;
				update();
			}
		}
		
		public function get blurAmount():int {
			return m_blurAmount;
		}
 
		protected function createMaskBox():void {
			//如果 m_window 里面有名为 mc_mask 的 MC，
			//则使用此为 mask，若没有，则创建一个 mask
			if (m_window[MASK_NAME]) {
				m_maskBox = m_window[MASK_NAME];
			}else {
				var maskRef:Class = Object(m_window).constructor;
				m_maskBox = new maskRef();
			}
			m_blurredImage.mask = m_maskBox;
			m_maskBox.visible = false;
			m_window.addChild(m_maskBox);
		}
		
		protected function createBlurImage():void {
			m_blurredImageData = new BitmapData(m_background.width, m_background.height, false);
			m_blurredImageData.draw(m_background);
			m_blurredImageData.applyFilter(m_blurredImageData, m_blurredImageData.rect, new Point(), m_blur);
			m_blurredImage = new Bitmap(m_blurredImageData, "auto", true);
			//坐标转换
			var newPoint:Point = m_container.globalToLocal(new Point(m_background.x, m_background.y));
			m_blurredImage.x = newPoint.x;
			m_blurredImage.y = newPoint.y;
			m_container.addChild(m_blurredImage);
		}
 
		protected function update():void {
			m_container.removeChild(m_blurredImage);
			m_blurredImageData.dispose();
			createBlurImage();
			m_blurredImage.mask = m_maskBox;
		}
	}
}